package com.example.controller;

import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: easy_demo
 * @description:
 * @author: ChenZhiXiang
 * @create: 2019-06-18 10:12
 **/
@RestController
public class MainController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;

    /**
     * 动图gif
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/images/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
    }

    @RequestMapping("/images/captcha1")
    public void captcha1(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(4,request,response);
    }

    @RequestMapping("/images/captcha2")
    public void captcha2(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.outPng(request,response);
    }

    @GetMapping("/login")
    public JsonResult login(String username,String code){
        if (!CaptchaUtil.ver(code,request)){
            return JsonResult.error("验证码错误");
        }
        CaptchaUtil.clear(request);
        return JsonResult.ok("验证码正确");
    }
}
